import {Component, ElementRef, ViewChild} from '@angular/core';
import {IonicPage, NavController, NavParams, ViewController} from 'ionic-angular';
import {StorageProvider} from "@providers/storage";
import {CustomerProvider} from "@providers/interface/customer";

/**
 * Generated class for the OrderMapPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

declare let AMap;

@IonicPage()
@Component({
  selector: 'page-order-map',
  templateUrl: 'order-map.html',
})
export class OrderMapPage {

  @ViewChild('map_container') map_container: ElementRef;

  map: any;//地图对象
  marker: any;
  position: any;

  userInfo: any = this.storage.read('userInfo');

  data: any = this.navParams.data;

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              private viewCtrl: ViewController,
              private storage: StorageProvider,
              private customer: CustomerProvider) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad OrderMapPage');
    this.map = new AMap.Map(this.map_container.nativeElement, {
      view: new AMap.View2D({//创建地图二维视口
        zoom: 18, //设置地图缩放级别
        rotateEnable: true,
        resizeEnable: true,
        showBuildingBlock: true
      })
    });
    this.customer.getMarsterLocation({
      "ORDERID": this.data.ID
    }).then((message) => {
      this.position = [message.data.LONGITUDE, message.data.LATITUDE];
      this.map.setCenter(this.position);
      let icon: string;
      if (this.data.DJLX == '送货') {
        icon = "assets/qcfw/images/truck.png";
      } else {
        icon = "assets/qcfw/images/driver.png";
      }
      this.marker = new AMap.Marker({
        icon: icon,
        position: this.map.getCenter(),
        title: '我的服务'
      });

      this.marker.setMap(this.map);
      this.marker.setTitle('师傅当前位置');
      this.marker.setLabel({
        //修改label相对于maker的位置
        offset: new AMap.Pixel(20, 20),
        content: "<div class='info'>师傅当前位置</div>"
      });
    }).catch(() => {
    });

  }

  dismiss(data ?: any) {
    this.viewCtrl.dismiss(data);
  }

}
